package com.wooribank.android.tablet;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.wooribank.android.tablet.accountCheck.AllFragment;
import com.wooribank.android.tablet.accountCheck.DepositFragment;
import com.wooribank.android.tablet.accountCheck.ForeignCurrencyFragment;
import com.wooribank.android.tablet.accountCheck.FundFragment;
import com.wooribank.android.tablet.accountCheck.LoanFragment;

public class AccountCheckFragment extends Fragment implements OnTabChangeListener {

	private View mRoot;
	private TabHost mTabHost;
	private int mCurrentTab;

	public static final String TAB_DEPOSIT = "deposit";
	public static final String TAB_LOAN = "loan";
	public static final String TAB_FOREIGN_CURRENCY = "foreignCurrency";
	public static final String TAB_FUND = "fund";
	public static final String TAB_ALL_ACCOUNT = "allAccount";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mRoot = inflater.inflate(R.layout.account_check_fragment, null);
		mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
		setupTabs();
		return mRoot;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);
		mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTab(mCurrentTab);
		updateTab(TAB_DEPOSIT, R.id.depositContentFrameLayout);
	}

	private void setupTabs() {
		mTabHost.setup();
		mTabHost.addTab(newTab(TAB_DEPOSIT, R.string.deposit_trust, R.id.depositContentFrameLayout));
		mTabHost.addTab(newTab(TAB_LOAN, R.string.loan, R.id.loanContentFrameLayout));
		mTabHost.addTab(newTab(TAB_FOREIGN_CURRENCY, R.string.foreign_currency,
				R.id.foreignCurrencyContentFrameLayout));
		mTabHost.addTab(newTab(TAB_FUND, R.string.fund, R.id.fundContentFrameLayout));
		mTabHost.addTab(newTab(TAB_ALL_ACCOUNT, R.string.all_account, R.id.allContentFrameLayout));
	}

	private TabSpec newTab(String tag, int labelId, int tabContentId) {
		TextView textView = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.account_check_tab,
				(ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
		textView.setText(getString(labelId));
		TabSpec tabSpec = mTabHost.newTabSpec(tag);
		tabSpec.setIndicator(textView);
		tabSpec.setContent(tabContentId);
		return tabSpec;
	}

	@Override
	public void onTabChanged(String tabId) {
		if (TAB_DEPOSIT.equals(tabId)) {
			updateTab(tabId, R.id.depositContentFrameLayout);
			mCurrentTab = 0;
			return;
		}
		if (TAB_LOAN.equals(tabId)) {
			updateTab(tabId, R.id.loanContentFrameLayout);
			mCurrentTab = 1;
			return;
		}
		if (TAB_FOREIGN_CURRENCY.equals(tabId)) {
			updateTab(tabId, R.id.foreignCurrencyContentFrameLayout);
			mCurrentTab = 2;
			return;
		}
		if (TAB_FUND.equals(tabId)) {
			updateTab(tabId, R.id.fundContentFrameLayout);
			mCurrentTab = 3;
			return;
		}
		if (TAB_ALL_ACCOUNT.equals(tabId)) {
			updateTab(tabId, R.id.allContentFrameLayout);
			mCurrentTab = 4;
			return;
		}
	}

	private void updateTab(String tabId, int placeholder) {
		FragmentManager fm = getFragmentManager();
		Fragment fragment = null;
		if (fm.findFragmentByTag(tabId) == null) {
			if (tabId == TAB_DEPOSIT)
				fragment = new DepositFragment();
			else if (tabId == TAB_LOAN)
				fragment = new LoanFragment();
			else if (tabId == TAB_FOREIGN_CURRENCY)
				fragment = new ForeignCurrencyFragment();
			else if (tabId == TAB_FUND)
				fragment = new FundFragment();
			else if (tabId == TAB_ALL_ACCOUNT)
				fragment = new AllFragment();
			fm.beginTransaction().replace(placeholder, fragment, tabId).commit();
		}

	}
}
